//
//  HomeMainViewModel.m
//  AppleMarket
//
//  Created by yangy on 18/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "HomeMainViewModel.h"

// viewModel
// model
#import "BannerModel.h"
#import "NewsModel.h"

@implementation HomeMainViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        @weakify(self);
        [self.refershBannerCommond.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            if(x == nil){
                [self.refershEndSubject sendError:nil];
            }
            if([[x objectForKey:@"Status"] integerValue] == 1){
                NSMutableArray *tempArray = [NSMutableArray array];
                [[x objectForKey:@"Data"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [tempArray addObject:[BannerModel yy_modelWithDictionary:obj]];
                }];
                self.bannerArray = [tempArray copy];
                [self.refershEndSubject sendNext:nil];
            }else{
                [self.refershEndSubject sendError:x[@"Msg"]];
            }
            
        }];
        
        [self.refershNewsCommond.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            if(x == nil){
                [self.refershNewsEndSubject sendNext:nil];
            }
            if([[x objectForKey:@"Status"] integerValue] == 1){
                NSArray *dataArray = [[x objectForKey:@"Data"] objectForKey:@"AnnouncementList"];
                self.newsArray = [[dataArray.rac_sequence.signal map:^id _Nullable(id  _Nullable value) {
                    return [NewsModel yy_modelWithDictionary:value];
                }] toArray];
                [self.refershNewsEndSubject sendNext:nil];
            }else{
                [self.refershNewsEndSubject sendNext:nil];
            }
        }];
    }
    return self;
}

- (RACCommand *)refershBannerCommond
{
    if(!_refershBannerCommond){
        _refershBannerCommond = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 请求网络数据
                [NetRequest request_GetBannerWithCompleteHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _refershBannerCommond;
}

- (RACCommand *)refershNewsCommond
{
    if(!_refershNewsCommond){
        _refershNewsCommond = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                // 请求网络数据
                [NetRequest request_GetNewsListWithCompleteHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _refershNewsCommond;
}

- (RACSubject *)refershEndSubject
{
    if(!_refershEndSubject){
        _refershEndSubject = [RACSubject subject];
    }
    return _refershEndSubject;
}

- (RACSubject *)refershNewsEndSubject
{
    if(!_refershNewsEndSubject){
        _refershNewsEndSubject = [RACSubject subject];
    }
    return _refershNewsEndSubject;
}

- (RACSubject *)newsClickSubject
{
    if(!_newsClickSubject){
        _newsClickSubject = [RACSubject subject];
    }
    return _newsClickSubject;
}

- (RACSubject *)bannerClickSubject
{
    if(!_bannerClickSubject){
        _bannerClickSubject = [RACSubject subject];
    }
    return _bannerClickSubject;
}

- (UnLoginViewModel *)assetsVM
{
    if(!_assetsVM){
        _assetsVM = [[UnLoginViewModel alloc] init];
    }
    return _assetsVM;
}

- (AssetViewModel *)coinVM
{
    if(!_coinVM){
        _coinVM = [[AssetViewModel alloc] init];
    }
    return _coinVM;
}

- (MarkListViewModel *)marketVM
{
    if(!_marketVM){
        _marketVM = [[MarkListViewModel alloc] init];
    }
    return _marketVM;
}

- (RACSubject *)moreClickSubject
{
    if(!_moreClickSubject){
        _moreClickSubject = [RACSubject subject];
    }
    return _moreClickSubject;
}
@end
